package com.pachong.android.frameworkbase.customviews.edittext;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import com.pachong.android.frameworkbase.R;
import com.pachong.android.frameworkbase.customviews.IconTextArrowEditText;
import com.pachong.android.frameworkbase.utils.DimensionUtil;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/7/1
 * 发送验证码后可以实现 SendCheckable 接口,回调可以设置该textview的相关属性 如背景色等。
 */
public class CaptchaEditText extends IconTextArrowEditText implements View.OnClickListener {
    private TextView mtvCountDown;

    private LayoutParams lytTvcountParams = null;
    private String btnText;
    private CountDownTimer mCountDownTimer;
    int mBtnHeight;
    int mBtnWidth;
    float mTvTextSize;
    int mNormalTextColor;
    int mCountDownTextColor;
    int mNormalBackGround;
    int mCountDownBackGround;


    int mCountDownNumber = 60;

    public void setCountDownNumber(int mCountDownNumber) {
        this.mCountDownNumber = mCountDownNumber;
    }

    public void setChackableimpl(SendCheckable mChackableimpl) {
        this.mChackableimpl = mChackableimpl;
    }

    SendCheckable mChackableimpl;

    enum CountdownState {
        NORMAL,//正常状态
        COUNTING,//计时中
        END//计时完
    }

    private void setState(CountdownState state) {
        this.mState = state;
        setText();
    }

    CountdownState mState;

    public CaptchaEditText(Context context) {
        super(context);
        setupViews();
    }

    public CaptchaEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupViews(attrs);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setupViews(AttributeSet attrs) {
        mtvCountDown = new TextView(getContext());
        mtvCountDown.setOnClickListener(this);
        initTvcount(attrs);
        mtvCountDown.setBackground(getResources().getDrawable(mNormalBackGround));
        mtvCountDown.setTextSize(TypedValue.COMPLEX_UNIT_PX,mTvTextSize);
        setState(CountdownState.NORMAL);
        addSubView(mtvCountDown, lytTvcountParams, Gravity.CENTER_VERTICAL | Gravity.RIGHT);
    }

    private void setupViews()
    {
        mNormalTextColor=getResources().getColor(R.color.tv_captchaedit_normal_textcolor);
        mCountDownTextColor=getResources().getColor(R.color.tv_captchaedit_countdown_textcolor);
        mBtnHeight= DimensionUtil.dip2px(getContext(), 34);
        mBtnWidth=DimensionUtil.dip2px(getContext(), 118);
        mNormalBackGround=R.drawable.bg_captchaedit;
        mTvTextSize=R.dimen.text_tiny;
        mtvCountDown.setGravity(Gravity.CENTER);
        setState(CountdownState.NORMAL);
        lytTvcountParams = new LayoutParams(mBtnWidth, mBtnHeight);
    }

    private void initTvcount(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CaptchaEditText);
            mNormalTextColor = a.getColor(R.styleable.CaptchaEditText_normalTextColor, getResources().getColor(R.color.tv_captchaedit_normal_textcolor));
            mCountDownTextColor = a.getColor(R.styleable.CaptchaEditText_coundingTextColor, getResources().getColor(R.color.tv_captchaedit_countdown_textcolor));
            mBtnHeight = (int) a.getDimension(R.styleable.CaptchaEditText_captchaBtnHeight, DimensionUtil.dip2px(getContext(), 34));
            mBtnWidth = (int) a.getDimension(R.styleable.CaptchaEditText_captchaBtnWidth, DimensionUtil.dip2px(getContext(), 118));
            mNormalBackGround = a.getResourceId(R.styleable.CaptchaEditText_normalBackground, R.drawable.bg_captchaedit);
            mTvTextSize = a.getDimension(R.styleable.CaptchaEditText_captchaTextSize, R.dimen.text_tiny);
            a.recycle();
        }
        mtvCountDown.setGravity(Gravity.CENTER);
        setState(CountdownState.NORMAL);
        lytTvcountParams = new LayoutParams(mBtnWidth, mBtnHeight);
    }

    private void setText() {
        if (mState == CountdownState.NORMAL || mState == CountdownState.END) {
            mtvCountDown.setTextColor(mNormalTextColor);
            if (mState == CountdownState.END) {
                mtvCountDown.setText(R.string.get_code_again);
            } else mtvCountDown.setText(R.string.get_code);

        } else {
            mtvCountDown.setTextColor(mCountDownTextColor);
            counding();
        }
    }

    private void counding() {
        mtvCountDown.setEnabled(false);
        mCountDownTimer = new CountDownTimer(mCountDownNumber * 1000, 1000) {
            int time = mCountDownNumber;

            @Override
            public void onTick(long millisUntilFinished) {
                String temp = getContext().getString(R.string.countdowning, --time + "");
                mtvCountDown.setText(temp);
            }

            @Override
            public void onFinish() {
                setState(CountdownState.END);
                mtvCountDown.setEnabled(true);
            }
        }.start();
    }

    @Override
    public void onClick(View v) {
        setState(CountdownState.COUNTING);
        try {
            mChackableimpl.sendCheckCode(mtvCountDown);
        } catch (NullPointerException e) {
        }
    }

    public interface SendCheckable {
        void sendCheckCode(TextView btnCount);
    }
}
